﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Use this to change the brightness of a pic
    /// </summary>
    public class Brightness : BaseImageFilter
    {
        protected int m_nBrightness = 10;

        /// <summary>
        /// default constructor
        /// </summary>
        public Brightness()
            : this(10)
        {
        }

        /// <summary>
        /// primary constructor
        /// </summary>
        /// <param name="_nBrightness"></param>
        public Brightness(int _nBrightness)
        {
            m_nBrightness = _nBrightness;
        }

        /// <summary>
        /// used by cloning
        /// </summary>
        /// <param name="_brightness"></param>
        internal Brightness(Brightness _brightness)
        {
            this.m_nBrightness = _brightness.m_nBrightness;
        }

        /// <summary>
        /// Gets or sets intensity
        /// </summary>
        public int Intensity
        {
            get { return m_nBrightness; }
            set { m_nBrightness = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            if (m_nBrightness < -255 || m_nBrightness > 255)
            {
                return _bitmap;
            }

            UnsafeBitmap bitmap = _bitmap;
            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = bitmap.BitmapData; // bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;

            int nVal = 0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;

                int nOffset = bitmap.OffsetPointerIncrease; //stride - bitmap.Width * 3;
                int nWidth = bitmap.Width * bitmap.BytesPerPixelCount;//bitmap.Width * 3;

                for (int y = 0; y < bitmap.Height; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        nVal = (int)(p[0] + m_nBrightness);

                        if (nVal < 0) nVal = 0;
                        if (nVal > 255) nVal = 255;

                        p[0] = (byte)nVal;

                        ++p;
                    }
                    p += nOffset;
                }
            }
            return _bitmap; //bitmap.UnlockBits(bmData);
        }

        /// <summary>
        /// use this to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Brightness(this);
        }
    }
}
